import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routes"

Vue.use(VueRouter)

const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location,onResolve,onReject){
    if(onResolve || onReject) return originPush.call(this,location,onResolve,onReject)
    return originPush.call(this,location).catch(err=>{err})
}

const originReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location,onResolve,onReject){
    if(onResolve || onReject) return originReplace.call(this,location,onResolve,onReject)
    return originReplace.call(this,location).catch(err=>{err})
}


const router = new VueRouter({
    mode:'history',
    routes,
    scrollBehavior(to,from,savedPosition){
        // to 表示到达的目标对象
        // from 表示来自哪个路由对象
        // savedPostion记录上一个页面位置
        if(savedPosition){
            return savedPosition
        }else{
            return {x:0,y:0}
        }
    }
})

export default router